There are several cases when one would want to check the number of CPUs in Ubuntu such as checking cores before creating a VM or you want to see how powerful your system is.
So in this tutorial, I will walk you through the following ways to check the number of CPUs in Ubuntu:
- Using lscpu command
- Using nproc command
- By printing the file content of the /proc/cpuinfo file
- Using the dmidecode command
Let's start with the first one.
1. Using the lscpu command
The lscpu command is used to get installed processors info in variety and due to its rich feature set, it is the first preference by many users to get CPU info.
It comes pre-installed on most Linux systems and can be used without any additional options:
And as you can see, it gives tonnes of additional information which may not be what every user wants.
In that case, you can use the egrep in the following manner to sort the output:
lscpu | egrep 'Model name|Socket|Thread|NUMA|CPU\(s\)'
As you can see, I have a CPU with 6 cores (but it shows 12. Then how?)
If you notice, thread per core is 2 so the total number will be multiplied by 2 which gave me 12 cores.
And if you're looking for physical cores, just divide CPU cores by the number of threads.
2. Using the nproc command
The whole purpose of using the nproc command is to get the number of cores. So if you're looking for the easiest option, here you have it.
To get the total number of cpu cores, simply execute
nproc in your terminal:
3. By reading the file content of the /proc/cpuinfo file
This method gives similar output to the first method but with more details.
And being a file, all you need is the cat command to print its file contents:
4. Using the dmidecode command
The dmidecode command uses the DMI (Desktop Management Interface) table of your system to retrieve information about your system hardware.
As it gives tonnes of additional information when used without any options, it is advised to use some flags to get the desired output:
sudo dmidecode -t 4 | egrep -i 'core (count|enabled)|thread count|Version'
As you can see, it gives the number of physical cores (Core Count) and the total number of cores including threads.
This means it's close to perfect output for most users. A good way to check CPU count in Linux.
Get more details about your CPU
If you want additional information apart from CPU cores, we made a dedicated tutorial on that topic:
And get some details on current CPU usage:
I hope you will find this guide helpful.