Skip to Main Content

Make Apt-Get Always Run as Root


Linux only: If you have ever tried to use the apt-get command to install software but received an error because you forgot sudo, the Command-line Fu site has a quick but useful tip for you.

The tip is one of those really simple, but extremely useful time-savers that might have just passed you by otherwise—instead of constantly typing "sudo" before each of the apt-get commands, simply use the alias function to add it there automatically:

alias apt-get='sudo apt-get'

To make this change persistent across sessions, you will want to add this line to your ~/.bashrc file. Experienced readers will note that you can always use the sudo !! command to run the last typed command as root whenever you forget the "sudo" the first time.

Don't forget to check out our top 10 command line tools, 10 handy bash aliases, or you can learn how to use grep.

Update: Reader Stephen writes in to mention that some apt-get commands like "apt-get source" don't require root access, and will actually function incorrectly—so keep that in mind if you use those. Otherwise you can create an alias for "sudo apt-get install" and use it only for installing, like commenter Alex Price mentions below.

Always run apt-get as root [Command-line Fu]